package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.R;
import com.itheima.reggie.dto.SetmealDto;

import com.itheima.reggie.entity.Setmeal;
import com.itheima.reggie.service.SetmealDishService;
import com.itheima.reggie.service.SetmealService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

@RestController
@Slf4j
@RequestMapping("/setmeal")
public class SetmealController {
    @Autowired
    private SetmealService setmealService;
    @Autowired
    private SetmealDishService setmealDishService;


    @PostMapping//新增套餐
    public R<String> save(@RequestBody SetmealDto setmealDto) {
        setmealService.saveWithSetmeal(setmealDto);
        return R.success("添加成功");
    }

    @GetMapping("/page")//套餐分页查看
    public R<Page> page(int page, int pageSize, String name) {
        Page<Setmeal> setmealPage = new Page<>(page, pageSize);
        LambdaQueryWrapper<Setmeal> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(StringUtils.isNotEmpty(name), Setmeal::getName, name);
        wrapper.orderByAsc(Setmeal::getUpdateTime);
        setmealService.page(setmealPage, wrapper);

        Page<SetmealDto> setmealDtoPage = new Page<>();
        BeanUtils.copyProperties(setmealPage, setmealDtoPage);
        List<Setmeal> records = setmealPage.getRecords();
        List<SetmealDto> list = records.stream().map((item) -> {
            SetmealDto setmealDto = new SetmealDto();
            BeanUtils.copyProperties(item, setmealDto);

            Long categoryId = item.getCategoryId();
            Setmeal setmeal = setmealService.getById(categoryId);
            if (setmeal != null) {
                String categoryName = setmealDto.getCategoryName();
                setmealDto.setCategoryName(categoryName);
            }
            return setmealDto;
        }).collect(Collectors.toList());
        setmealDtoPage.setRecords(list);
        return R.success(setmealDtoPage);
    }

    @DeleteMapping//套餐删除
    public R<String> delete(@RequestParam List<Long> ids) {
        setmealService.removeWithSetmeal(ids);
        return R.success("删除成功");
    }

    @PostMapping("/status/{status}")//批量修改套餐状态，起售，停售
    public R<String> update(@PathVariable int status, @RequestParam List<Long> ids) {
        log.info(ids.toString());
        log.info(status + "");

        for (int i = 0; i < ids.size(); i++) {
            //根据id获取套餐对象
            Setmeal setmeal = setmealService.getById(ids.get(i));
            //将套餐对象的status属性的值，设置为status
            setmeal.setStatus(status);
            //调用service层方法 修改状态值
            setmealService.updateById(setmeal);

        }

        return R.success("修改状态成功");
    }

    @GetMapping("/{id}")//套餐回显
    public R<SetmealDto> get(@PathVariable Long id) {
        log.info("id:{}", id);
        SetmealDto setmealDto = setmealService.getWithSetmeal(id);
        return R.success(setmealDto);
    }

    //修改菜品
    @PutMapping
    public R<String> updateWithDish(@RequestBody SetmealDto setmealDto) {
        log.info(setmealDto.toString());
        setmealService.updateWithDish(setmealDto);
        return R.success("套餐修改成功");
    }
}
